package org.example.project_12.controller;

import org.example.project_12.dto.CommentDto;
import org.example.project_12.entity.Comment;
import org.example.project_12.service.CommentService;
import org.example.project_12.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @PostMapping
    public Result<Comment> addComment(@RequestBody CommentDto dto, @RequestHeader("userId") Long userId) {
        return Result.ok(commentService.addComment(dto.getDocId(), userId, dto.getContent(), dto.getParentId()));
    }

    @GetMapping("/list")
    public Result<List<Comment>> getComments(@RequestParam Long docId) {
        return Result.ok(commentService.getCommentsByDoc(docId));
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteComment(@PathVariable Long id, @RequestHeader("userId") Long userId, @RequestHeader(value = "isAdmin", defaultValue = "false") boolean isAdmin) {
        commentService.deleteComment(id, userId, isAdmin);
        return Result.ok(null);
    }
} 